#include "vm.h"

#include <windows.h>
#include "resource.h"

#define CLASS_NAME		"Fili_GL_test"
#define WINDOW_TITLE	"Fili's GL test"

HINSTANCE	vm_hInstance = NULL;
HWND		vm_hWnd = NULL;
int			vm_zoom = 4;

extern void PaintWindow(HDC hdc);

void setZoom(int zoom)
{
	vm_zoom = zoom;

	int nWidth = SCREEN_WIDTH * zoom;
	int nHeight = SCREEN_HEIGHT * SCREEN_COUNT * zoom;
	
	RECT rcClient, rcWindow;
	POINT ptDiff;

	GetClientRect(vm_hWnd, &rcClient);
	GetWindowRect(vm_hWnd, &rcWindow);
	ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
	ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
	MoveWindow(vm_hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);

	switch (zoom)
	{
	case 1:
		CheckMenuRadioItem(GetMenu(vm_hWnd), ID_VIEW_ZOOMX1, ID_VIEW_ZOOMX8, ID_VIEW_ZOOMX1, MF_BYCOMMAND);
		break;
	case 2:
		CheckMenuRadioItem(GetMenu(vm_hWnd), ID_VIEW_ZOOMX1, ID_VIEW_ZOOMX8, ID_VIEW_ZOOMX2, MF_BYCOMMAND);
		break;
	case 4:
		CheckMenuRadioItem(GetMenu(vm_hWnd), ID_VIEW_ZOOMX1, ID_VIEW_ZOOMX8, ID_VIEW_ZOOMX4, MF_BYCOMMAND);
		break;
	case 8:
		CheckMenuRadioItem(GetMenu(vm_hWnd), ID_VIEW_ZOOMX1, ID_VIEW_ZOOMX8, ID_VIEW_ZOOMX8, MF_BYCOMMAND);
		break;
	default:
		break;
	}
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
	case WM_COMMAND:		
		switch (LOWORD(wParam))
		{
		case ID_FILE_EXIT:
			DestroyWindow(hWnd);
			break;
		case ID_VIEW_ZOOMX1:
			setZoom(1);
			break;
		case ID_VIEW_ZOOMX2:
			setZoom(2);
			break;
		case ID_VIEW_ZOOMX4:
			setZoom(4);
			break;
		case ID_VIEW_ZOOMX8:
			setZoom(8);
			break;
		case ID_HELP_ABOUT:
//			DialogBox(vm_hInstance, (LPCTSTR)IDD_ABOUTBOX, vm_hWnd, (DLGPROC)About);
			MessageBox(vm_hWnd, "Fili's GL test\n(c)2008", WINDOW_TITLE, MB_OK);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
		// Paint our window
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hWnd, &ps);
			PaintWindow(hdc);			
			EndPaint(hWnd, &ps);			
		}
		break;

		// Kill window
	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


bool initWindow()
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= vm_hInstance;
	wcex.hIcon			= NULL;//LoadIcon(hInstance, (LPCTSTR)IDI_WINDEMO);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszMenuName	= (LPCSTR)IDR_MAINMENU;
	wcex.lpszClassName	= CLASS_NAME;
	wcex.hIconSm		= NULL;//LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	if (!RegisterClassEx(&wcex))
		return FALSE;

	// Create a window
	vm_hWnd = CreateWindow(
		CLASS_NAME, 
		WINDOW_TITLE, 
		WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |WS_MINIMIZEBOX,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
		NULL, 
		NULL, 
		vm_hInstance, 
		NULL);

	if (!vm_hWnd)
		return FALSE;

	setZoom(vm_zoom);
	// Cause first show of window
	ShowWindow(vm_hWnd, SW_SHOW);
	UpdateWindow(vm_hWnd);

	return TRUE;
}

void initDisplay()
{

}

int main(int argc, char **argv)
{
	HACCEL hAccelTable;

	vm_hInstance = GetModuleHandle(NULL);

	hAccelTable = LoadAccelerators(vm_hInstance, (LPCTSTR)IDR_MAINACC);


	initWindow();
	initDisplay();

	MSG msg;
	while (1)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (GetMessage(&msg, NULL, 0, 0)) 
			{
				if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
			}
			else
			{
				return msg.wParam;
			}
		}
		else
		{
//			vm_update();
		}
	}

	return msg.wParam;

	return 0;
}